import time

import pyautogui

from my_package.xqol_utils import resource_path, find_btn_location_window

# 菜单按钮
menu_btn_img_path = resource_path("images/screen_img/菜单按钮.png")
# 菜单栏右侧对应的取消按钮
cancel_btn_img_path = resource_path("images/screen_img/取消按钮.png")
# 备忘按钮
memo_btn_img_path = resource_path("images/screen_img/备忘按钮.png")

# 最左下角的确认按钮
# 最右下角的取消按钮

def find_menu_btn(window, click=True):
    return find_and_click_button(window, menu_btn_img_path, "菜单", click)


def find_memorandum_btn(window, click=True):
    return find_and_click_button(window, memo_btn_img_path, "备忘", click)


def find_cancel_btn(window, click=True):
    return find_and_click_button(window, cancel_btn_img_path, "取消", click)


def find_left_bottom_confirm_btn(window, click=True):
    return find_and_click_button(window, memo_btn_img_path, "左下角确认按钮", click)


def find_right_bottom_cancel_btn(window, click=True):
    return find_and_click_button(window, memo_btn_img_path, "右下角取消按钮", click)


def find_and_click_button(window, btn_img_path, btn_name, click=True):
    """
    查找并点击指定按钮

    :param window: 窗口对象
    :param btn_img_path: 按钮图片路径
    :param btn_name: 按钮名称（用于日志输出）
    :param click: 是否点击
    :return: True - 找到并点击；False - 未找到
    """
    # 激活窗口
    try:
        time.sleep(0.5)
        # 将窗口激活并置顶
        window.activate()
    except Exception as e:
        print("窗口操作失败：", e)
    time.sleep(0.5)
    res = find_btn_location_window(window, btn_img_path)
    if not res:
        #print(f"未找到【{btn_name}】按钮")
        return False
    if click:
        print(window.title, f"点击【{btn_name}】按钮")
        pyautogui.click(window.left + res[0] + 5, window.top + res[1] + 5)
    return True